<template>
  <div>
    <el-table :data="items">
        <!-- <el-table-column prop="date" label="日期" width="140">
        </el-table-column> -->
        <el-table-column prop="eventId" label="ID" width="260">
        </el-table-column>
        <el-table-column prop="title" label="文章标题">
        </el-table-column>
        <el-table-column prop="type" label="类别">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="180">
      <template slot-scope="scope">
        <el-button type="text" size="small" @click="$router.push(`/events/edit/${scope.row.eventId}`)">编辑</el-button>
        <el-button type="text" size="small" @click="remove(scope.row)">删除</el-button>
      </template>
    </el-table-column>
      </el-table>
      <el-pagination
        layout="prev, pager, next"
        :page-size="size"
        @current-change="current_change"
        :total="total">
      </el-pagination>
  </div>
</template>
<script>
export default {
  data(){
    return{
      items:[],
      total:0,
      size:0
    }
  },
  created() {
    this.fetch()
  },
  methods:{
    async fetch(){
      const res =await this.$http.get('/events')
      this.items=res.data.infos.list
      this.total=res.data.infos.total
      this.size=res.data.infos.size
      this.items.forEach(item => {
        if (item.type === 0) {
          item.type = "比赛";
        } else {
          item.type = "活动";
        }
      });
    },
    async remove(row) {
      this.$confirm(`此操作将永久删除该文章"${row.title}", 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await this.$http.delete(`event/${row.eventId}`)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.fetch()
        })       
    },
    async current_change(page){
      console.log(page)
      const res =await this.$http.get(`/events?pn=${page}`)
      this.items=res.data.infos.list
      this.total=res.data.infos.total
      this.items.forEach(item => {
        if (item.type === 0) {
          item.type = "比赛";
        } else {
          item.type = "活动";
        }
      });
    }
  }
}
</script>

